<?php

use think\facade\Db;
use think\migration\Migrator;
use think\migration\db\Column;

class SystemBaseDatas extends Migrator
{
    private string $NowTime;


    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change(): void
    {
        $this->NowTime = time();
        $this->_insertAdmin();
        $this->_insertAdminRole();
        $this->_insertRouteMenu();
        $this->_insertSystemConfig();
        $this->_insertHooks();
    }

    private function _insertAdmin(): void
    {
        $data  = [
            'username'    => 'admin',
            'password'    => md5(sha1('admin888')),
            'nickname'    => '管理员',
            'status'      => 1,
            'create_time' => time(),
            'update_time' => time(),
        ];
        $exist = Db::name('admin_user')->where('id', 1)->value('id');
        if (!$exist) {
            $this->table('admin_user')->insert([$data])->save();
        }
    }

    private function _insertAdminRole(): void
    {
        $data  = [
            [
                'role_name'  => '超级管理员',
                'role_code'  => 'admin',
                'allow_auth' => '[]',
                'pid'        => 0,
                'status'     => 1,
                'rank'       => 999
            ]
        ];
        $exist = Db::name('admin_role')->where('id', 1)->value('id');
        if (!$exist) {
            $this->table('admin_role')->insert($data)->save();
        }
        $data  = [
            'role_id' => 1,
            'user_id' => 1
        ];
        $exist = Db::name('admin_role_access')->where('user_id', 1)->value('user_id');
        if (!$exist) {
            $this->table('admin_role_access')->insert([$data])->save();
        }
    }

    private function _insertRouteMenu(): void
    {
        $rows  = [
            [
                'menu_name'      => '首页',
                'menu_type'      => 'menu',
                'pid'            => 0,
                'menu_tree'      => '0,1',
                'route_name'     => 'home',
                'route_path'     => '/home',
                'component_path' => 'home/index',
                'icon'           => 'iconfont icon-shouye',
                'roles'          => "[\"admin\"]",
                'rank'           => 0,
                'status'         => 0,
            ], [
                'menu_name'      => '系统管理',
                'menu_type'      => 'menu',
                'pid'            => 0,
                'menu_tree'      => '0,2',
                'route_name'     => 'system',
                'route_path'     => '/system',
                'redirect_url'   => '',
                'component_path' => '',
                'icon'           => 'ri:settings-2-line',
                'roles'          => "[\"admin\"]",
                'rank'           => 1,
                'status'         => 1,
            ], [
                'menu_name'      => '菜单管理',
                'menu_type'      => 'menu',
                'pid'            => 2,
                'menu_tree'      => '0,2,3',
                'route_name'     => 'systemMenu',
                'route_path'     => '/system/menu',
                'component_path' => 'system/menu/index',
                'icon'           => 'ep:menu',
                'roles'          => "[\"admin\"]",
                'rank'           => 2,
                'status'         => 1,
            ], [
                'menu_name'      => '角色管理',
                'menu_type'      => 'menu',
                'pid'            => 2,
                'menu_tree'      => '0,2,4',
                'route_name'     => 'systemRole',
                'route_path'     => '/system/role',
                'component_path' => 'system/role/index',
                'icon'           => 'ri:admin-fill',
                'roles'          => "[\"admin\"]",
                'rank'           => 3,
                'status'         => 1,
            ], [
                'menu_name'      => '用户管理',
                'menu_type'      => 'menu',
                'pid'            => 2,
                'menu_tree'      => '0,2,5',
                'route_name'     => 'systemUser',
                'route_path'     => '/system/user',
                'component_path' => 'system/user/index',
                'icon'           => 'ri:admin-line',
                'roles'          => "[\"admin\"]",
                'rank'           => 4,
                'status'         => 1,
            ], [
                'menu_name'      => '部门管理',
                'menu_type'      => 'menu',
                'pid'            => 2,
                'menu_tree'      => '0,2,6',
                'route_name'     => 'systemDept',
                'route_path'     => '/system/dept',
                'component_path' => 'system/dept/index',
                'icon'           => 'ri:git-branch-line',
                'roles'          => "[\"admin\"]",
                'rank'           => 5,
                'status'         => 1
            ], [
                'menu_name'       => '增加菜单',
                'menu_type'       => 'btn',
                'pid'             => 3,
                'menu_tree'       => '0,2,3,7',
                'permission_code' => 'Menu@List.AddMenu',
                'status'          => 1,
            ], [
                'menu_name'       => '编辑菜单',
                'menu_type'       => 'btn',
                'pid'             => 3,
                'menu_tree'       => '0,2,3,8',
                'permission_code' => 'Menu@List.EditMenu',
                'status'          => 1,
            ], [
                'menu_name'       => '删除菜单',
                'menu_type'       => 'btn',
                'pid'             => 3,
                'menu_tree'       => '0,2,3,9',
                'permission_code' => 'Menu@List.DelMenu',
                'status'          => 1,
            ], [
                'menu_name'       => '增加按钮',
                'menu_type'       => 'btn',
                'pid'             => 3,
                'menu_tree'       => '0,2,3,10',
                'permission_code' => 'Menu@List.AddBtn',
                'status'          => 1,
            ], [
                'menu_name'       => '增加角色',
                'menu_type'       => 'btn',
                'pid'             => 4,
                'menu_tree'       => '0,2,4,11',
                'permission_code' => 'Role@List.Add',
                'status'          => 1,
            ], [
                'menu_name'       => '编辑角色',
                'menu_type'       => 'btn',
                'pid'             => 4,
                'menu_tree'       => '0,2,4,12',
                'permission_code' => 'Role@List.Edit',
                'status'          => 1,
            ], [
                'menu_name'       => '删除角色',
                'menu_type'       => 'btn',
                'pid'             => 4,
                'menu_tree'       => '0,2,4,13',
                'permission_code' => 'Role@List.Delete',
                'status'          => 1,
            ], [
                'menu_name'       => '角色授权',
                'menu_type'       => 'btn',
                'pid'             => 4,
                'menu_tree'       => '0,2,4,14',
                'permission_code' => 'Role@List.Permission',
                'status'          => 1,
            ],
            //增加、编辑、删除用户
            [
                'menu_name'       => '增加用户',
                'menu_type'       => 'btn',
                'pid'             => 5,
                'menu_tree'       => '0,2,5,15',
                'permission_code' => 'User@List.Add',
                'status'          => 1,
            ], [
                'menu_name'       => '编辑用户',
                'menu_type'       => 'btn',
                'pid'             => 5,
                'menu_tree'       => '0,2,5,16',
                'permission_code' => 'User@List.Edit',
                'status'          => 1,
            ], [
                'menu_name'       => '删除用户',
                'menu_type'       => 'btn',
                'pid'             => 5,
                'menu_tree'       => '0,2,5,17',
                'permission_code' => 'User@List.Delete',
                'status'          => 1,
            ], [
                'menu_name'       => '重置密码',
                'menu_type'       => 'btn',
                'pid'             => 5,
                'menu_tree'       => '0,2,5,18',
                'permission_code' => 'User@List.ResetPwd'
            ],
            // 增加、编辑、删除部门
            [
                'menu_name'       => '增加部门',
                'menu_type'       => 'btn',
                'pid'             => 6,
                'menu_tree'       => '0,2,6,19',
                'permission_code' => 'Dept@List.Add',
                'status'          => 1,
            ], [
                'menu_name'       => '编辑部门',
                'menu_type'       => 'btn',
                'pid'             => 6,
                'menu_tree'       => '0,2,6,20',
                'permission_code' => 'Dept@List.Edit',
                'status'          => 1,
            ], [
                'menu_name'       => '删除部门',
                'menu_type'       => 'btn',
                'pid'             => 6,
                'menu_tree'       => '0,2,6,21',
                'permission_code' => 'Dept@List.Delete',
                'status'          => 1,
            ], [
                'menu_name'      => '系统配置',
                'menu_type'      => 'menu',
                'pid'            => 2,
                'menu_tree'      => '0,2,22',
                'route_name'     => 'systemConfig',
                'route_path'     => '/system/config',
                'component_path' => 'system/config/index',
                'icon'           => 'ri:list-settings-line',
                'roles'          => "[\"admin\"]",
                'rank'           => 0,
                'status'         => 1
            ]
        ];
        $exist = Db::name('route_menu')->where('id', '<', 22)->value('id');
        if (!$exist) {
            $this->table('route_menu')->insert($rows)->saveData();
        }
    }

    private function _insertSystemConfig(): void
    {
        $systemConfigGroup = [
            [
                'group_name' => '基本配置'
            ], [
                'group_name' => '中控界面配置'
            ], [
                'group_name' => '安全配置'
            ], [
                'group_name' => '上传配置'
            ]
        ];
        $this->table('config_group')->insert($systemConfigGroup)->saveData();

        $systemConfig = [
            [
                'group_id'        => 1,
                'config_name'     => '系统名称',
                'config_variable' => 'system_title',
                'config_value'    => 'WiCMS',
                'config_type'     => 'text',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 99,
                'status'          => 1
            ],
            [
                'group_id'        => 2,
                'config_name'     => '中控显示标题',
                'config_variable' => 'Title',
                'config_value'    => 'WiCMS管理中心',
                'config_type'     => 'text',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 99,
                'status'          => 1
            ],
            [
                'group_id'        => 2,
                'config_name'     => '系统版本号',
                'config_variable' => 'Version',
                'config_value'    => '5.8.0',
                'config_type'     => 'text',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 5,
                'status'          => 1
            ], [
                'group_id'        => 2,
                'config_variable' => 'FixedHeader',
                'config_name'     => '固定头部',
                'config_value'    => '1',
                'config_type'     => 'radio',
                'config_options'  => '1:是|0:否',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 98,
                'status'          => 1
            ],
            // HiddenSideBar
            [
                'group_id'        => 2,
                'config_variable' => 'HiddenSideBar',
                'config_name'     => '侧边栏隐藏',
                'config_value'    => '0',
                'config_type'     => 'radio',
                'config_options'  => '1:是|0:否',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 97,
                'status'          => 1
            ],
            // MultiTagsCache
            [
                'group_id'        => 2,
                'config_variable' => 'MultiTagsCache',
                'config_name'     => '多标签页缓存',
                'config_value'    => '0',
                'config_type'     => 'radio',
                'config_options'  => '1:是|0:否',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 96,
                'status'          => 1
            ],
            // KeepAlive
            [
                'group_id'        => 2,
                'config_variable' => 'KeepAlive',
                'config_name'     => '页面缓存',
                'config_value'    => '1',
                'config_type'     => 'radio',
                'config_options'  => '1:是|0:否',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 95,
                'status'          => 1
            ],
            // Locale
            [
                'group_id'        => 2,
                'config_variable' => 'Locale',
                'config_name'     => '默认语言',
                'config_value'    => 'zh',
                'config_type'     => 'select',
                'config_options'  => 'zh:简体中文|en:英语',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 94,
                'status'          => 1
            ],
            // 默认主题
            [
                'group_id'        => 2,
                'config_variable' => 'Theme',
                'config_name'     => '默认主题',
                'config_value'    => 'light',
                'config_type'     => 'select',
                'config_options'  => 'auto:自动|light:亮色|dark:暗黑',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 93,
                'status'          => 1
            ],
            // 默认风格
            [
                'group_id'        => 2,
                'config_variable' => 'Layout',
                'config_name'     => '导航菜单模式',
                'config_value'    => 'vertical',
                'config_type'     => 'select',
                'config_options'  => 'vertical:纵向|horizontal:横向|mix:混合',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 92,
                'status'          => 1
            ],
            // HideTabs
            [
                'group_id'        => 2,
                'config_variable' => 'HideTabs',
                'config_name'     => '隐藏标签页',
                'config_value'    => '0',
                'config_type'     => 'radio',
                'config_options'  => '1:是|0:否',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 91,
                'status'          => 1
            ],
            // 默认主题颜色
            [
                'group_id'        => 2,
                'config_variable' => 'EpThemeColor',
                'config_name'     => '默认主题颜色',
                'config_value'    => '#409EFF',
                'config_type'     => 'color_picker',
                'config_options'  => '#ffffff:light|#1b2a47:default|#722ed1:saucePurple|#eb2f96:pink|#f5222d:dusk|#fa541c:volcano|#13c2c2:mingQing|#52c41a:auroraGreen',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 90,
                'status'          => 1
            ],
            // HideFooter
            [
                'group_id'        => 2,
                'config_variable' => 'HideFooter',
                'config_name'     => '是否隐藏页脚',
                'config_value'    => '0',
                'config_type'     => 'radio',
                'config_options'  => '1:是|0:否',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 89,
                'status'          => 1
            ],
            // ShowLogo
            [
                'group_id'        => 2,
                'config_variable' => 'ShowLogo',
                'config_name'     => '是否显示logo',
                'config_value'    => '1',
                'config_type'     => 'radio',
                'config_options'  => '1:是|0:否',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 88,
                'status'          => 1
            ],
            // ShowModel
            [
                'group_id'        => 2,
                'config_variable' => 'ShowModel',
                'config_name'     => '标签页风格',
                'config_value'    => 'smart',
                'config_type'     => 'radio',
                'config_options'  => 'smart:灵动|card:卡片|google:谷歌风格',
                'create_time'     => $this->NowTime,
                'update_time'     => $this->NowTime,
                'rank'            => 87,
                'status'          => 1
            ],
        ];
        $this->table('config_list')->insert($systemConfig)->saveData();
    }

    private function _insertHooks(): void
    {
        // 系统钩子
        $systemHooks = [
            [
                'hook_name'   => 'AppInit',
                'hook_desc'   => '应用初始化标签位',
                'hook_type'   => 0,
                'hook_param'  => '无',
                'hook_load'   => 1,
                'hook_status' => 1,
                'create_time' => $this->NowTime,
                'update_time' => $this->NowTime,
            ], [
                'hook_name'   => 'HttpRun',
                'hook_desc'   => '应用开始标签位	',
                'hook_type'   => 0,
                'hook_param'  => '无',
                'hook_load'   => 1,
                'hook_status' => 1,
                'create_time' => $this->NowTime,
                'update_time' => $this->NowTime,
            ], [
                'hook_name'   => 'HttpEnd',
                'hook_desc'   => '应用结束标签位',
                'hook_type'   => 0,
                'hook_param'  => '当前响应对象实例',
                'hook_load'   => 1,
                'hook_status' => 1,
                'create_time' => $this->NowTime,
                'update_time' => $this->NowTime,
            ], [
                'hook_name'   => 'LogWrite',
                'hook_desc'   => '日志write方法标签位',
                'hook_type'   => 0,
                'hook_param'  => '当前写入的日志信息',
                'hook_load'   => 1,
                'hook_status' => 1,
                'create_time' => $this->NowTime,
                'update_time' => $this->NowTime,
            ], [
                'hook_name'   => 'RouteLoaded',
                'hook_desc'   => '路由加载完成',
                'hook_type'   => 0,
                'hook_param'  => '无',
                'hook_load'   => 1,
                'hook_status' => 1,
                'create_time' => $this->NowTime,
                'update_time' => $this->NowTime,
            ], [
                'hook_name'   => 'LogRecord',
                'hook_desc'   => '日志记录	',
                'hook_type'   => 0,
                'hook_param'  => '无',
                'hook_load'   => 1,
                'hook_status' => 1,
                'create_time' => $this->NowTime,
                'update_time' => $this->NowTime,
            ]
        ];
        // 业务钩子
        $businessHooks = [
            [
                'hook_name'   => 'AdminUserLoginSuccess',
                'hook_desc'   => '后台登录',
                'hook_type'   => 1,
                'hook_param'  => '管理员信息',
                'hook_load'   => 1,
                'hook_status' => 1,
                'create_time' => $this->NowTime,
                'update_time' => $this->NowTime,
            ]
        ];
        $exist         = Db::name('hooks')->where('id', '<', 7)->value('id');
        if (!$exist) {
            $this->table('hooks')->insert(array_merge($systemHooks, $businessHooks))->saveData();
        }
    }

}
